using System.Linq;
using Sustainalytics.ESG.Entities;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.Comprehensive
{
    public class ControversyChangeRule : ComprehensiveRule<RPControversyIndicator>
    {
        private readonly int[] _lowerControversy = { 0, 1, 2 };

        private readonly int[] _upperControversy = { 3, 4, 5 };

        public override Severity Severity => Severity.Yellow;
        public override BusinessResearchProduct BusinessResearchProduct => BusinessResearchProduct.Comprehensive;
        public override string Code => "YR1";
        public override string InformationalMessage => "The Highest Controversy Level of a company moves by three levels";

        public ControversyChangeRule()
        {
            For(x => x.AnswerCategory).When(Validator).WithKey(x => x.Id).WithMessage("The company {0} #{1}# does not have compliant highest controversy");
        }

        private bool Validator(
            RPControversyIndicator validatedControversyIndicator, 
            RPControversyIndicator referenceControversyIndicator)
        {
            var modelValue = (int)GetValue(validatedControversyIndicator);
            var referenceValue = (int)GetValue(referenceControversyIndicator);

            return !((_lowerControversy.Contains(modelValue) && _upperControversy.Contains(referenceValue)) || (_upperControversy.Contains(modelValue) && _lowerControversy.Contains(referenceValue)));
        }
    }
}